package cn.cgj.controller;

import cn.cgj.domain.BusinessInfo;
import cn.cgj.flowable.service.FlowableService;
import cn.cgj.service.BusinessInfoService;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
public class BusinessInfoController {

    /**
     * 日志
     */
    private static final Logger log = LoggerFactory.getLogger(BusinessInfoController.class);

    /**
     * 业务信息服务
     */
    @Autowired
    private BusinessInfoService businessInfoService;

    /**
     * 流程服务
     */
    @Autowired
    private FlowableService flowableService;

    @GetMapping
    public String index(HttpServletRequest request) {
        this.requestLog(request, "/", "index: 跳转首页", "无", "首页");
        return "index";
    }

    @ResponseBody
    @GetMapping("/list")
    public ResponseEntity<List<BusinessInfo>> list(HttpServletRequest request, BusinessInfo businessInfo) {
        List<BusinessInfo> list = businessInfoService.selectBusinessInfoList(businessInfo);
        this.requestLog(request, "/list", "list: 查询数据", businessInfo.toString(), list.toString());
        return ResponseEntity.ok(list);
    }

    @ResponseBody
    @GetMapping("/start")
    public ResponseEntity<?> start(HttpServletRequest request, String key) {
        ProcessInstance processInstance = flowableService.startProcess("akf");
        String processId = processInstance.getId();
        businessInfoService.insertBusinessInfo(new BusinessInfo().setProcessId(processId)
                .setType(processInstance.getProcessDefinitionName()).setIsDelete(0).setNode(flowableService.createFlowChart(processId)));
        this.requestLog(request, "/start", "start: 启动流程", key, processId);
        return ResponseEntity.ok(processId);
    }

    @ResponseBody
    @GetMapping("/audit/{id}/{processId}/{outcome}")
    public ResponseEntity<?> audit(HttpServletRequest request
            , @PathVariable("id") Long id
            , @PathVariable("processId") String processId
            , @PathVariable("outcome") String outcome) {
        flowableService.auditTask(processId, new HashMap<String, Object>(){{ put("outcome", outcome);}});
        String flowChart = flowableService.createFlowChart(processId);
        BusinessInfo info = new BusinessInfo().setId(id).setNode(flowChart);
        if (flowChart.matches("^流程已完成【.*】")) {
            info.setIsDelete(1);
        }
        businessInfoService.updateBusinessInfo(info);
        this.requestLog(request, "/audit", "audit: 审核任务"
                , String.format("{id: %s, processId: %s, outcome: %s}", id, processId, outcome), outcome);
        return ResponseEntity.ok(outcome);
    }

    public void requestLog(HttpServletRequest request, String path, String method, String reqData, String data) {
        log.info("\n访问用户【{}】\n访问地址【{}】\n访问方法【{}】\n请求参数【{}】\n响应数据【{}】", request.getRemoteAddr(), path, method, reqData, data);
    }

}
